﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm_Mes.CheckManagement
{
    public partial class CheckWay : DevExpress.XtraEditors.XtraForm
    {
        // 分页参数
        private int pageIndex = 1;
        private int pageSize = 10;
        private int total = 0;

        public CheckWay()
        {
            InitializeComponent();
            this.Load += CheckWay_Load;
            checkButton1.Click += BtnQuery_Click; // 查询
            checkButton2.Click += checkButton2_Click; // 新增
            checkButton3.Click += checkButton3_Click; // 编辑
        }

        // 数据结构
        public class CheckWayDto
        {
            public long id { get; set; }
            public string checkCode { get; set; }
            public string checkWayName { get; set; }
            public int count { get; set; }
            public bool 选择 { get; set; } // 单选框
        }
        public class PageResult<T>
        {
            public int total { get; set; }
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public List<T> items { get; set; }
            public int code { get; set; }
            public string message { get; set; }
            public object data { get; set; }
        }

        public class ApiResult<T>
        {
            public int code { get; set; }
            public string message { get; set; }
            public T data { get; set; }
        }

        // 加载数据
        private async void CheckWay_Load(object sender, EventArgs e)
        {
            // 设置GridView列的FieldName，确保数据绑定正确
            检查编号.FieldName = "checkCode";
            检测名称.FieldName = "checkWayName";
            await LoadDataAsync();
        }

        // 查询按钮事件
        private async void BtnQuery_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            await LoadDataAsync();
        }

        // 新增按钮事件
        private async void checkButton2_Click(object sender, EventArgs e)
        {
            using (var dlg = new AddCheckWayForm())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    await LoadDataAsync();
                }
            }
        }

        // 编辑按钮事件
        private async void checkButton3_Click(object sender, EventArgs e)
        {

            long? selectedId = null;
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                if ((bool)(gridView1.GetRowCellValue(i, "选择") ?? false))
                {
                    var row = gridView1.GetRow(i) as CheckWayDto;
                    if (row != null)
                    {
                        selectedId = row.id;
                        break;
                    }
                }
            }
            if (selectedId == null)
            {
                MessageBox.Show("请先选择一行！");
                return;
            }

            // 查询详情
            CheckWayDto detail = null;
            using (HttpClient client = new HttpClient())
            {
                var url = $"http://localhost:5295/api/CheckWay/CheckWayModel?id={selectedId}";
                var apiResult = await client.GetFromJsonAsync<ApiResult<CheckWayDto>>(url);
                detail = apiResult.data;
            }

            // 弹窗并回显
            using (var dlg = new AddCheckWayForm())
            {
                dlg.SetModel(detail);
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    await LoadDataAsync();
                }
            }
        }

        // API调用与数据绑定
        private async Task LoadDataAsync()
        {
            string code = textBox1.Text.Trim();
            string name = textBox2.Text.Trim();
            string url = $"http://localhost:5295/api/CheckWay/GetAllCheckWay?PageIndex={pageIndex}&PageSize={pageSize}&CheckCode={code}&CheckWayName={name}";
            using (HttpClient client = new HttpClient())
            {
                var result = await client.GetFromJsonAsync<PageResult<CheckWayDto>>(url);
                gridControl1.DataSource = result.items; // 绑定 items 字段
                total = result.total;
            }
            // 可在panelControl2底部加Label显示总条数和分页按钮
        }

        private void 检验编号_Click(object sender, EventArgs e) { }
        private void gridSplitContainer1Grid_Click(object sender, EventArgs e) { }
        private void gridControl1_Click(object sender, EventArgs e) { }

        private void CheckWay_Load_1(object sender, EventArgs e)
        {

        }
    }
}